package com.caiyi.financial.nirvana.sms.gateway;

import com.alibaba.fastjson.JSON;
import com.caiyi.financial.nirvana.sms.jedis.JedisKey;
import com.caiyi.financial.nirvana.sms.jedis.JedisService;
import com.caiyi.financial.nirvana.sms.queue.NotifyQueueEntity;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.List;

/**
 * 网关行为线程
 */
public class GatewayNotifyThread implements Runnable {

    private static final Logger logger = LoggerFactory.getLogger(GatewayNotifyThread.class);
    private boolean start = true;

    @Override
    public void run() {
        while (start) {
            try {
                List<String> list = JedisService.brpop(5, JedisKey.SMS_QUEUE_NOTIFY_STATUS);
                if (list == null || list.isEmpty()) {
                    continue;
                }
                String value = list.get(1);
                logger.info("GatewayNotifyThread run . key : {}, value : {}", list.get(0), value);
                NotifyQueueEntity notify = JSON.parseObject(value, NotifyQueueEntity.class);
                GateWayThread thread = Controller.getGateWayThread(notify.getGatewayId());
                if (thread == null || thread.getGateWay() == null) {
                    logger.error("GatewayNotifyThread is null ! value : {}", value);
                    continue;
                }
                boolean bl = thread.getGateWay().notifyStatus(notify);
                if (!bl) {
                    logger.error("GatewayNotifyThread notifyStatus error ! value : {}", value);
                }
            } catch (Exception e) {
                try {
                    Thread.sleep(1000);
                } catch (Exception e1) {
                }
                logger.error("GatewayNotifyThread error !!! ", e);
            }
        }
    }
}
